using UnityEngine;
using System.Collections;

public enum HittingEffect
{
    BlINKING
}

public class BeingHittedEffect : MonoBehaviour
{
    public static BeingHittedEffect instance;
    void Awake()
    {
        instance = this;
       
    }

    public void Show(HittingEffect effect,Transform target)
    {
        StartCoroutine(_Show( effect ,target));
    }


    IEnumerator _Show(HittingEffect effect,Transform target)
    {
        SkinnedMeshRenderer[] renderers = target.GetComponentsInChildren<SkinnedMeshRenderer>();
        float offset = 1.0f;
        StartCoroutine(ChangeTexOffset(renderers,offset));
        yield return null;
    }

    public float firstDuration = 0.015f;
    public float secondDuration = 0.15f;
    public float delta = 0.1f;
    IEnumerator ChangeTexOffset(SkinnedMeshRenderer[] renderers,float offset)
    {
        float firstSpeed = 1.0f/firstDuration;
        float secondSpeed = 1.0f/secondDuration;
        while(true)
        {
            offset -= firstSpeed * Time.fixedDeltaTime;

            if(offset  < delta)
            {
                offset = 0f;
				foreach(SkinnedMeshRenderer r  in renderers)
				{           
					r.material.SetFloat("_TexOffset",offset);
				}
                break;
            }
            foreach(SkinnedMeshRenderer r  in renderers)
            {           
                r.material.SetFloat("_TexOffset",offset);
            }
            yield return null;
        }

        while(true)
        {
            offset += secondSpeed * Time.fixedDeltaTime;
            if(offset >= 1.0f )
            {
                offset = 1.0f;
                foreach(SkinnedMeshRenderer r  in renderers)
                {          
                    r.material.SetFloat("_TexOffset",offset);
                }
                break;
            }

            foreach(SkinnedMeshRenderer r  in renderers)
            {           
                r.material.SetFloat("_TexOffset",offset);
            }
            yield return null;
        }

    }

}
